﻿using System;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ones.ServiceUserAuthentication;

namespace Ones
{
    public partial class Login : System.Web.UI.Page
    {
        private ServiceUserAuthenticationClient uac = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            uac = new ServiceUserAuthenticationClient("authenBasicHttpBinding");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (uac.UserAuthentiaction(txtUsername.Text, txtPassword.Text))
            {
                //FormsAuthentication.SetAuthCookie(txtUsername.Text, true);
                //Response.Redirect("/SysAdmin/Accounts.aspx");
                string userrole = uac.UserRole(txtUsername.Text);
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, userrole);
                
                string encryptedStr = FormsAuthentication.Encrypt(ticket);
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedStr);
                Response.Cookies.Add(cookie);
                
                ViewMembershipUserEmployee user = uac.getUser(txtUsername.Text);
                Session["user"] = user;

                if (userrole == "SysAdmin")
                {
                    Response.Redirect("/SysAdmin/Index.aspx");
                }
                else if (userrole == "WebAdmin")
                {
                    Response.Redirect("/WebAdmin/Index.aspx");
                }
                else if (userrole == "Approver")
                {
                    Response.Redirect("/Editorial/Approver/Index.aspx");
                }
                else if (userrole == "Editor")
                {
                    Response.Redirect("/Editorial/Editor/Index.aspx");
                }
                else if (userrole == "Writer")
                {
                    Response.Redirect("/Editorial/Writer/Index.aspx");
                }
            }
            else
            {
            }
        }
    }
}